package com.demo.xxljobclient.controller;

import com.demo.xxljobclient.po.User;
import com.demo.xxljobclient.services.UserServices;
import com.power.common.model.CommonResult;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author peter
 * @date 2022/2/26 19:08
 */
@RestController
@Slf4j
public class IndexController {

    @Resource
    private UserServices userServices;

    @GetMapping("foo")
    public CommonResult foo() {
        log.info("[IndexController.foo]: access");
        ArrayList<User> allUsers = userServices.getAllUsers();
        return CommonResult.ok().setResult(allUsers);
    }

    @XxlJob("print")
    public void job() {
        // 获取 xxl-job 运行参数
        String jobParam = XxlJobHelper.getJobParam();

        // 记录日志
        log.info("getJobLogFileName: {}", XxlJobHelper.getJobLogFileName());
        XxlJobHelper.log("XxlJobHelper [IndexController.job] jobParam: {}", jobParam);

        // XxlJobHelper.handleFail("任务失败");
        XxlJobHelper.handleSuccess("任务完成");
    }

}
